﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace YK.ListItemTransfer
{
    public class Log
    {
        const string LogFileFolder = "Log";
        private readonly string _LogFile;

        public Log(string logFile)
        {
            _LogFile = logFile;
            CreateLogFile();
        }

        private void CreateLogFile()
        {
            File.Create(LogFilePath).Close();
        }

        private string LogFilePath
        {
            get
            {
                return string.Format("{0}/{1}/{2}.txt", Application.StartupPath, LogFileFolder, _LogFile);
            }
        }

        private StreamWriter GetStream()
        {
            return new StreamWriter(LogFilePath, true);
        }

        public void WriteLog(Exception exception, string message)
        {
            try
            {
                StreamWriter writer = GetStream();
                writer.WriteLine(ParseMessage(exception, message));
                writer.Close();
            }
            catch
            { }
        }

        private string ParseMessage(Exception exception, string message)
        {
            return string.Format("Custom message: {1}{0}Error details: {2}{0}Date time: {3}{0}", Environment.NewLine, message, exception.Message, DateTime.Now);
        }

        internal string GetLogFilePath()
        {
            return LogFilePath;
        }
    }
}
